package main

import (
	"fmt"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/validator/v10"
	uuid "github.com/satori/go.uuid"
	"log"
	"os"
	"os/signal"
	"sub_web/src/global"
	"sub_web/src/initalize"
	utils "sub_web/src/util"
	"sub_web/src/util/register/consul"
	myvarlidator "sub_web/src/varlidator"
	"syscall"
)

func main() {
	baseRouter := initalize.InitRouter()
	if err := utils.InitTrans("zh"); err != nil {
		log.Fatalf("init trans failed, err:%v\n", err)
		return
	}

	initalize.InitConfig()
	initalize.InitConn()
	initalize.InitRouter()

	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		_ = v.RegisterValidation("mobile", myvarlidator.CheakMobile)
		_ = v.RegisterValidation("identify", myvarlidator.CheakIdentify)
	}

	register_client := consul.NewRegistryClient(global.SrvConfig.ConsulConf.Host, global.SrvConfig.ConsulConf.Port)
	serviceId := fmt.Sprintf("%s", uuid.NewV4())
	err := register_client.Register(global.SrvConfig.Host, global.SrvConfig.Port, global.SrvConfig.Name, global.SrvConfig.Tags, serviceId)
	if err != nil {
		panic(err)
	}

	go func() {
		err = baseRouter.Run(fmt.Sprintf("%s:%d", global.SrvConfig.Host, global.SrvConfig.Port))
		if err != nil {
			panic(err)
		}
	}()

	quit := make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit
	if err = register_client.DeRegister(serviceId); err != nil {
		panic(err)
	} else {
		fmt.Println("注销成功")
	}
}
